package com.qfedu.repo.conntoller;

import com.alibaba.fastjson.JSON;
import com.qfedu.repo.bean.Manager;
import com.qfedu.repo.bean.RespResuit;
import com.qfedu.repo.common.Const;
import com.qfedu.repo.dao.Impl.ManagerDaoImpl;
import com.qfedu.repo.dao.ManagerDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/manager")
public class MangerServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        RespResuit result = null;
        switch (method) {
            case "login":
                result = login(request, response);
                break;
            case "logout":
                logout(request, response);
                break;
            default:
                break;
        }
        PrintWriter writer = response.getWriter();
        writer.write(JSON.toJSONString(result));
        writer.flush();
        writer.close();
    }

    private void logout(HttpServletRequest request, HttpServletResponse response) {

    }

    /**
     * 登录方法
     *
     * @param request
     * @param response
     * @throws IOException
     */
    private RespResuit login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        RespResuit result = new RespResuit();
        String account = request.getParameter("account");
        String pwd = request.getParameter("pwd");
        PrintWriter writer = null;
        try {
            if (account.length() < 4 || account.length() > 12) {
                result.setCode(Const.LOGIN_FAIL);
                result.setMsg("参数不合法");
                writer = response.getWriter();
                writer.write("参数不合法");
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            if (pwd.length() < 4 || pwd.length() > 12) {
                result.setCode(Const.LOGIN_FAIL);
                result.setMsg("参数不合法");
                writer = response.getWriter();
                writer.write("参数不合法");
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        ManagerDao managerDao = new ManagerDaoImpl();
        Manager manager = managerDao.login(account, pwd);
        System.out.println(manager);
        if (manager == null) {
            //返回失败的结果
            result.setCode(Const.LOGIN_FAIL);
            result.setMsg("failed");
        } else {
            //返回给前端
            result.setCode(Const.LOGIN_SUCCESS);
            result.setMsg("success");
            result.setData(JSON.toJSONString(manager));
        }
        return result;
    }
}
